$(function(){
    //获取地址的id
    var url = location.href;
    console.log(url);
    var params = url.substr(url.indexOf("?") + 1);
    console.log(params);
    var id = params.split("=")[1];
    // console.log(id);
    $.ajax({
        type:"get",
        url:"/product/queryProductDetail",
        data:{
            "id":id
        },
        success:function(res){
            // console.log(res);
            var html = template("detailTpl",{result:res});
            $(".detail").html(html);
            //获得slider插件对象
            var gallery = mui('.mui-slider');
            gallery.slider({
                
                interval:5000//自动轮播周期，若为0则不自动播放，默认为0；
            });
            mui(".mui-numbox").numbox()
        }
    });
    $(".detail").on("tap",".detail-item span",function(){
        // alert(1);
        $(this).addClass("active").siblings("span").removeClass("active");
    });
    //加入购物车
    $(".detail").on("click",".add-btn",function(){
        //获取数量
        var num = $(".mui-numbox-input").val();
        // console.log("1"+num);
        //获取选择的尺码
        var size = $(".detail-item span.active").text();
        //判断是否选择了尺码
        if(size == null){
            mui.alert("请选择尺码!");
            return;
        }
        // console.log(size);
        $.ajax({
            type:"post",
            url:"/cart/addCart",
            data:{
                "productId":id,
                "num":num,
                "size":size
            },
            dataType:"json",
            success:function(res){
                console.log(res);
                if(res.success){
                    //跳转购物车页面
                    location.href = "car.html";
                }
            }
        });
    });
})